//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_ui_moderation_init.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_add","_jailed","_all_players","_players","_format","_text","_index","_player_info","_player_id","_player_slot","_vehicles","_vec_var","_vec_type","_crew_count"
		,"_driver","_message","_message_text","_messages","_nearby","_nearby_count","_damage"];
_all_players = [];
GVAR(ui_moderation) = [];
GVAR(ui_vehicles) = [];
GVAR(ui_moderation_confirm) = [];
sleep 0.5;

// TODO implement permission on advanced functions
//ctrlEnable [9, true];

if (IsMultiplayer) then {
	_all_players = playableUnits;
} else {
	_all_players = [player];
};
TRACE_1("Player List",_all_players);
lbClear 1101;
lbAdd [1101,localize "STR_MSO_AA_DLG_NONE"];
_players = [objNull];
{
	_add = false;
	if (isplayer _x) then {
		_add = true;
		// if (!isnil "mp_rights_fnc_isMember") then {
			// _add = !((_x call mp_rights_fnc_isMember));
		// };
	};
	if (_add) then {
		_jailed = OGETVAR(_x,QGVAR(IS_PLAYER_JAILED),false);
		TRACE_1("Player Added",ARR_3(name _x, _jailed, vehicle _x));
		_index = GVAR(PlayerIndex) find (getPlayerUID _x);
		_player_info = [];
		_player_id = -1;
		if (_index >= 0) then {
			_player_info = GVAR(PlayerInfo) select _index;
			_player_id = _player_info select 3;
		};
		_player_slot = str _x;
		if (_player_id > 0) then {
			_player_slot = format["%1 (%2)",_player_id,_player_slot];
		};
		_format = "%1: %2 (%3)";
		if (_jailed) then {
			_format = "%6: %2 (%3)";
		} else {
			if (vehicle _x != _x) then {
				_format = "%1: %2 (%3) in %4 (%5)";
			};
		};
		_text = Format[_format,_player_slot, name _x, getPlayerUID _x, typeof (vehicle _x),str(vehicle _x),localize "STR_MSO_AA_DLG_JAILED"];
		TRACE_1("Player Text",_text);
		lbAdd [1101,_text];
		_players set [count _players, _x];
	} else {
		TRACE_1("Player Ignored",ARR_3(name _x, isplayer _x, vehicle _x));
	};
} foreach _all_players;
GVAR(ui_moderation) = _players;
lbSetCurSel [1101, 0];

lbclear 1102;
lbAdd [1102,localize "STR_MSO_AA_DLG_NONE"];
_vehicles = [objNull];
if (vehicle player != player && {(vehicle player) isKindOf "AllVehicles"}) then {
	_vec_var = vehicleVarName (vehicle player);
	if (_vec_var == "") then {
		_vec_var = localize "STR_MSO_AA_DLG_VEHICLE_MYVEHICLE";
	} else {
		_vec_var = localize "STR_MSO_AA_DLG_VEHICLE_MYVEHICLE" + " (" + _vec_var + ")";
	};
	_text = Format[localize "STR_MSO_AA_DLG_VEHICLE_DETAILS"
		,_vec_var
		,(typeOf (vehicle player))
		,mapgridPosition (getPosASL (vehicle player))
		,crew (vehicle player)
		,damage (vehicle player)
		,((vehicle player) nearEntities [["Man", "Car", "Motorcycle", "Tank"], 15])
		,driver (vehicle player)];
	TRACE_1("My Vehicle Text",_text);
	lbAdd [1102,_text];
	_vehicles set [count _vehicles, (vehicle player)];
};
{
	_add = true;
	if (side _x != playerSide && side _x != civilian) then {
		_add = false;
	};
	if (_add && _x isKindOf "AllVehicles") then {
		_vec_var = vehicleVarName _x;
		if (GVAR(MonitorNamedVehiclesOnly) == 1 && {_vec_var == ""}) then {
			TRACE_1("Unnamed vehicle",_x);
		} else {
			if (_vec_var == "") then { _vec_var = "----"; };
			_vec_type = [typeof _x, 0] call mso_fnc_getDisplayName;
			_crew_count = count (crew _x);
			_damage = damage _x;
			_nearby_count = 0;
			_nearby = (_x nearEntities [["Man", "Car", "Motorcycle", "Tank"], 15]);
			TRACE_1("Vehicle Text",_nearby);
			if (count _nearby > 0) then {
				_nearby_count = player countFriendly _nearby;
			};
			_driver = driver _x;
			if (isNull _driver) then {
				_driver = localize "STR_MSO_AA_NODRIVER";
			} else {
				_driver = name _driver;
			};
			_format = localize "STR_MSO_AA_DLG_VEHICLE_DETAILS";
			_text = Format[_format,_vec_var, _vec_type, mapgridPosition (getPosASL _x),_crew_count,_damage,_nearby_count,_driver];
			TRACE_1("Vehicle Text",_text);
			lbAdd [1102,_text];
			_vehicles set [count _vehicles, _x];
		};
	} else {
		TRACE_1("Vehicle Ignored",ARR_3(_x, typeof _x, side _x));
	};
} foreach vehicles;
GVAR(ui_vehicles) = _vehicles;
lbSetCurSel [1102, 0];

lbAdd [1104,localize "STR_MSO_AA_DLG_NONE"];
lbSetData [1104,0,""];
_messages = [""];
_index = 1;
while { (localize format["STR_MSO_AA_DLG_CANNED_%1",_index]) != "" } do {
	_message =(localize format["STR_MSO_AA_DLG_CANNED_%1",_index]);
	_message_text =(localize format["STR_MSO_AA_DLG_CANNED_TEXT_%1",_index]);
	lbAdd [1104,_message];
	lbSetData [1104,_index,_message_text];
	INC(_index);
};
lbSetCurSel [1104, 0];

TRACE("End");
TMFPROFILERSTOP;
